﻿using HomagChina.Basic.CCT.Db.CCHS_Db;
using HomagChina.CCHCS.Views;
using HomagGroup.Base.UI;
using Proxy;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Threading;
using static HomagChina.CCHCS.Utility;

namespace HomagChina.CCHCS.ViewModels
{
    public class StockViewModel : NotifyPropertyChangedBase
    {
        #region Handle

        private CommonHandle commonHandle;
        private DeleteInfoHandle Handle_Delete;
        private SearchInfoHandle Handle_Search;
        private InsertInfoHandle Handle_Insert;
        public RelayCommand StockInfoCommand { get; }
        public RelayCommand EditCommand { get; }
        public RelayCommand SearchCommand { get; }
        public RelayCommand UnLoadedCommand { get; }
        public RelayCommand LoadedCommand { get; }

        #endregion Handle

        #region 属性

        private string positioncode;

        public string Positioncode
        {
            get => positioncode;
            set { positioncode = value; Query(); OnPropertyChanged(); }
        }

        private bool _SelectedState = true;

        public bool SelectedState
        {
            get => _SelectedState;
            set { _SelectedState = value; OnPropertyChanged(); }
        }

        private ObservableCollection<object> _RemoveList = new ObservableCollection<object>();

        public ObservableCollection<object> RemoveList
        {
            get => _RemoveList;
            set
            {
                _RemoveList = value;
                OnPropertyChanged();
            }
        }

        private ObservableCollection<T_CCHS_Stock> _List_StockInfoModel;

        public ObservableCollection<T_CCHS_Stock> List_StockInfoModel
        {
            get => _List_StockInfoModel;
            set { _List_StockInfoModel = value; OnPropertyChanged(); }
        }

        public event EventHandler RequestScrollPositionSave;

        public event EventHandler RequestScrollPositionRestore;

        private DispatcherTimer UpdateSessionTimer;

        #endregion 属性

        #region 构造函数

        public StockViewModel()
        {
            commonHandle = new CommonHandle();
            Handle_Delete = new DeleteInfoHandle();
            Handle_Search = new SearchInfoHandle();
            Handle_Insert = new InsertInfoHandle();
            SearchCommand = new RelayCommand(Query);
            EditCommand = new RelayCommand(HandleEditCommand);
            StockInfoCommand = new RelayCommand(OpenStockInfo);
            //Query();
        }

        /// <summary>
        /// Unloaded
        ///
        /// </summary>
        private void ViewUnloadedCommand()
        {
            UpdateSessionTimer.Stop();
        }

        #endregion 构造函数

        #region 函数

        public void Query()
        {
            RequestScrollPositionSave?.Invoke(this, EventArgs.Empty);
            List_StockInfoModel = Handle_Search.GetStocksInfo(false, Positioncode);
            RequestScrollPositionRestore?.Invoke(this, EventArgs.Empty);
        }

        public void Return2Warehouse()
        {
            if (HomagGroup.Base.UI.Windows.MessageBox.Show(GetLocalText("Confirm return to warehouse"), GetLocalText("Prompt message"), MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                Handle_Insert.Return2Warehouse();
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        public void HandleEditCommand(object SelectItem)
        {
            StockInfoEditingViewModel stockInfoEditingViewModel = new StockInfoEditingViewModel(SelectItem as T_CCHS_Stock);
            UIAdjustments.CurrentThreadDialogManager.ShowDialog<StockInfoEditing>(stockInfoEditingViewModel, null);
            Query();
        }

        public void OpenStockInfo(object SelectItem)
        {
            var model = SelectItem as T_CCHS_Stock;
            StockInfoViewModel stockInfoViewModel = new StockInfoViewModel(model);

            StockInfo stockInfo = new StockInfo();
            stockInfoViewModel.RequestScrollPositionRestore += stockInfo.ViewModel_RequestScrollPositionRestore;
            stockInfoViewModel.RequestScrollPositionSave += stockInfo.ViewModel_RequestScrollPositionSave;
            stockInfo.DataContext = stockInfoViewModel;
            UIAdjustments.CurrentThreadDialogManager.ShowDialog(stockInfo);
            Query();
        }

        /// <summary>
        /// 处理删除命令
        /// </summary>
        /// <param name="obj"></param>
        public void Delete()
        {
            if (HomagGroup.Base.UI.Windows.MessageBox.Show(GetLocalText("Delete Stock"), GetLocalText("Prompt message"), MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                List<int> list = new List<int>();
                foreach (var item in RemoveList)
                {
                    list.Add((item as T_CCHS_Stock).Id);
                }
                Handle_Delete.ClearStockInfos(list);
                Query();
            }
        }
    }

    #endregion 函数
}